const { default: Toast } = require("../../../../../component/vant/toast/toast.js");

// pagesProperty/pages/oa/examine/detail/index.js
var app = getApp(), fetchApi = require("../../../../../utils/httpPost.js"), 
common_Js = require("../../../../../component/common.js");

import Dialog from '../../../../../component/vant/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    record_id:0,
    regRecord:{},
    isShowPopupEdit:false,
    submitState:false,
    checkState:0,
    admin_record_id:0,
    content:'',
    isEnd:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.data.record_id = parseInt(options.id);
    }
    this.onHttpInit()
  },
  
  preImg: function (t) {
    console.log("preSunImg",t)
    var a = t.currentTarget.dataset.imageindex, e = t.currentTarget.dataset.imgdata || [];
    var c = [];
    for(var index in e){
      c.push(e[index].imgUrl)
    }
    common_Js.preImg(a,c)
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/examine/approve/record/detail', {
      record_id: that.data.record_id,
    }, function (res) {
      if (res.data.code == 0) {
        if(res.data.data.record.lastRegAdminRecord!=undefined){
          that.data.admin_record_id = res.data.data.record.lastRegAdminRecord.id;
          that.data.isEnd = res.data.data.record.lastRegAdminRecord.handleSort == 2 ? 1 : 0;
        }
        that.setData({
          hasData: true,
          isEnd: that.data.isEnd,
          regRecord: res.data.data.record,
        })
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
  },
  handleCheck(){
    this.setData({
      isShowPopupEdit: !this.data.isShowPopupEdit
    })
  },
  onChangeRadioCheck(e){
    console.log('onChangeRadioCheck',e);
    this.data.checkState = e.detail;
    this.setData({
      checkState: e.detail,
    })
  },
  bindInput(e){
    console.log('bindInput',e);
    this.data.content = e.detail;
  },
  goHandleOp(e){
    console.log('goHandleOp',e);
    var type = e.currentTarget.dataset.type;
    if(type == 'edit'){

    } else if(type == 'reback'){
      var that = this;
      var message = "是否撤销该审批？"
      Dialog.confirm({
        title: '审批撤销',
        message
      }).then(() => {
        that.onHttpCancelOp();
      })
      .catch(() => {
        Dialog.close();
      });
    } else if(type == 'appraise'){
      
    } else if(type == 'check'){
      this.setData({
        isShowPopupEdit: true
      })
    }
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpCancelOp: function () {

    this.data.submitState = true;
    this.setData({
      submitState: true,
    })

    var that = this;
    fetchApi.httpPost('/examine/approve/cancel/record', {
      approve_record_id: that.data.record_id,
    }, function (res) {
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
      }else{
        Toast.fail(res.data.msg);
      }      
      that.data.submitState = false;
      that.setData({
        submitState: false,
      })
      that.onHttpInit();
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  submitOp: function () {
    if(this.data.checkState == 0){
      Toast("请选择审批状态");
      return;
    }

    if(this.data.isEnd == 0 && this.data.content == ''){
      Toast("请输入审核说明");
      return;
    }

    this.data.submitState = true;
    this.setData({
      submitState: true,
    })

    var that = this;
    fetchApi.httpPost('/examine/approve/handle/record', {
      approve_record_id: that.data.record_id,
      admin_record_id: that.data.admin_record_id,
      content: that.data.content,
      state: that.data.checkState,
    }, function (res) {
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
      }else{
        Toast.fail(res.data.msg);
      }      
      that.data.submitState = false;
      that.setData({
        submitState: false,
      })
      that.onHttpInit();
      that.handleCheck();
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
  },
})